package Annotation;

import org.junit.Test;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @Author: cjj
 * @Date: 2025/5/5 15:17
 */
public class AnnotationTest3 {
    @Test
    public void parseField() throws NoSuchFieldException{
        //1.获取类的字节码对象
        Class<BookStore> bookStoreClass = BookStore.class;
        //获取成员变量对象
        Field bookType = bookStoreClass.getDeclaredField("bookType");

        boolean flag = bookType.isAnnotationPresent(Book.class);

        if(flag){
            Book annotation = bookType.getAnnotation(Book.class);
            String name = annotation.bookName();
            double price = annotation.bookPrice();
            System.out.println(name+"--"+price);
        }
    }

    @Test
    public void parseMethod() throws NoSuchMethodException{
        //1.获取类的字节码对象
        Class<BookStore> bookStoreClass = BookStore.class;
        //2.获取方法的对象
        Method buyBookMethod = bookStoreClass.getDeclaredMethod("buyBook");

        boolean flag =buyBookMethod.isAnnotationPresent(Book.class);
       if(flag){
           Book annotation = buyBookMethod.getAnnotation(Book.class);
           String name = annotation.bookName();
           double price = annotation.bookPrice();
           System.out.println(name+"--"+price);


       }

    }
}
